const app = new getApp();
var time = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    team_list: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let promoter_id = 0;
    if (options.promoter_id) {
      promoter_id = options.promoter_id;
    }
    that.setData({
      promoter_id: promoter_id
    })

    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  loadInfo: function () {
    let that = this;
    let promoter_id = that.data.promoter_id;

    app.sendRequest({
      url: 'System.Distribution.myTeam',
      data: {
        promoter_id: promoter_id
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let team_list = app.checkEmpty(data, []);
          //delete team_list.array_self;
          for (let index in team_list) {
            //头像图片处理
            let list_child = app.checkEmpty(team_list[index], []);
            
            for (let key in list_child) {
              list_child[key].user_info = app.checkEmpty(list_child[key].user_info, {});
              list_child[key].user_info.user_headimg = app.IMG(list_child[key].user_info.user_headimg);

              let reg_time = list_child[key].user_info.reg_time;
              list_child[key].user_info.reg_time = time.formatTime(reg_time, 'Y-M-D h:m:s');
            }
            team_list[index] = list_child;
          }

          that.setData({
            team_list: team_list
          });
        }
      }
    });
  },

  /**
   * 头像加载失败
   */
  errorHeadImg: function (e) {
    let that = this;
    let default_img = that.data.default_img;
    let team_list = that.data.team_list;
    let index = e.currentTarget.dataset.index;
    let key = e.currentTarget.dataset.key;
    let img = team_list[index].user_info.user_headimg;
    let parm = {};
    let parm_key = "team_list[" + index + "].[" + key + "]user_info.user_headimg";

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_headimg;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  }
})